package com.aube.plugin.figure;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aube.plugin.origin.R;
import com.huyn.baseframework.ImageLoader;
import com.huyn.baseframework.ImageSizeDefineUtil;
import com.huyn.baseframework.dynamicload.DLLayout;
import com.huyn.baseframework.dynamicload.IPositionManager;
import com.huyn.baseframework.model.Character;
import com.huyn.baseframework.views.CircleImageView;

/**
 * Created by huyaonan on 16/5/23.
 */
public class GuestItem extends DLLayout {

    private LinearLayout mRoot;
    private CircleImageView mImg;
    private TextView mTitle, mDes;
    private View mDesLayout;
    private int mItemSize;

    public GuestItem(Context context) {
        this(context, null);
    }
    public GuestItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public GuestItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void initViews() {
        inflate(R.layout.item_guest, this, true);

        mRoot = (LinearLayout) findViewById(R.id.guest_group);
        mTitle = (TextView) findViewById(R.id.guest_name);
        mDes = (TextView) findViewById(R.id.guest_des);
        mDesLayout = findViewById(R.id.guest_des_layout);

        mDesLayout.setAlpha(0f);
    }

    public void resizeImgSize(int height) {
        mItemSize = height;
        mImg = new CircleImageView(context);
        LinearLayout.LayoutParams prams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
        mRoot.addView(mImg, 0, prams);
    }

    @Override
    public void fillData(Object o) {
        if(o == null || !(o instanceof Character))
            return;

        final Character character = (Character) o;
        mImg.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.bk_default));
        ImageLoader.getInstance(context).fillImage(mImg, ImageSizeDefineUtil.getImgUrl(character.actorPic, 200, 200));
        mTitle.setText(character.actorName);
        mDes.setText(character.actorDesc);
    }

    @Override
    public void notifyAnim(int animDelay) {
        mImg.setScaleX(0);
        mImg.setScaleY(0);

        ValueAnimator imgAnim = ValueAnimator.ofFloat(0, 1);
        imgAnim.setDuration(300);
        imgAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                setAlpha(value);
                mImg.setScaleX(value);
                mImg.setScaleY(value);
            }
        });
        if(animDelay > 0)
            imgAnim.setStartDelay(animDelay);

        ObjectAnimator txtAlpha = ObjectAnimator.ofFloat(mDesLayout, "alpha", 0, 1f);
        txtAlpha.setDuration(300);
        txtAlpha.setStartDelay(200+animDelay);

        imgAnim.start();
        txtAlpha.start();
    }

    @Override
    public void registToManager(IPositionManager iPositionManager) {

    }

    @Override
    public void unreistFromManager(IPositionManager iPositionManager) {

    }

    @Override
    public int[] getLocations() {
        return new int[0];
    }
}
